'''
Descripttion: 
Author: YeJianFa
Date: 2021-03-11 17:20:20
LastEditors: YeJianFa
LastEditTime: 2021-03-12 10:08:20
'''
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
from itemadapter import ItemAdapter
from scrapy.exporters import CsvItemExporter
import time
import copy
import json
class ZdmhotbtwdatePipeline:
    # def open_spider(self, spider):
    #     self.filename = '../file/' + time.strftime('%Y_%m_%d_%H_%M_%S')
    #     self.file = open(self.filename+".csv", "wb")
    #     self.exporter = CsvItemExporter(self.file,       
    #     fields_to_export=["title","author","hot", "date"])
    #     self.exporter.start_exporting()

    def process_item(self, item, spider):
        # self.exporter.export_item(item)
        data_item = copy.deepcopy(item) #深拷贝，防止数据重复、错乱
        spider.data.append(dict(data_item))
        #return item

    def close_spider(self,spider):
        #a = json.dumps(dict(spider.data))
        print(json.dumps(spider.data,ensure_ascii=False))
        # self.exporter.finish_exporting()
        # self.file.close()